1 00:00:00,300 --> 00:00:00,990 This video. 2 00:00:00,990 --> 00:00:02,550 I'm going to do some pathfinding. 3 00:00:02,550 --> 00:00:03,810 So I got this guy here. 4 00:00:03,810 --> 00:00:07,600 I named him Sly Dog, and then he's going to find that gold. 5 00:00:07,620 --> 00:00:10,690 There are no pre-programmed wave points. 6 00:00:10,710 --> 00:00:15,150 I'm just going to hit the play 5 seconds after the game starts up. 7 00:00:15,150 --> 00:00:16,200 He's going to start running. 8 00:00:16,200 --> 00:00:17,970 Well, it's going to start now, right? 9 00:00:17,970 --> 00:00:19,740 So we've got to start looking. 10 00:00:19,920 --> 00:00:21,000 There he goes. 11 00:00:21,570 --> 00:00:23,520 And he's finding his way. 12 00:00:23,520 --> 00:00:25,290 He's going to jump that little thing. 13 00:00:25,440 --> 00:00:27,120 And he found the gold. 14 00:00:27,120 --> 00:00:28,280 That's pretty cool. 15 00:00:28,290 --> 00:00:30,900 Pathfinding is a very handy tool. 16 00:00:30,900 --> 00:00:33,090 Let's go ahead and get started with that. 17 00:00:33,780 --> 00:00:34,050 All right. 18 00:00:34,050 --> 00:00:41,100 So I'm in my NPC work still and I deleted Sly Dog and I deleted the treasure so you could build that 19 00:00:41,100 --> 00:00:41,820 with me. 20 00:00:41,820 --> 00:00:44,310 I trust that you could put your own obstacles. 21 00:00:44,310 --> 00:00:48,390 You actually don't even really need it, but it's going to be boring if they don't run around if your 22 00:00:48,390 --> 00:00:49,920 little guy doesn't run around stuff. 23 00:00:49,920 --> 00:00:51,870 So let's go ahead and put our treasure. 24 00:00:51,870 --> 00:00:53,820 Basically, it's a part. 25 00:00:53,940 --> 00:00:55,140 There you go. 26 00:00:55,140 --> 00:00:56,760 And we'll call it treasure. 27 00:00:59,520 --> 00:01:04,920 And then make it like yellow or something like gold and then maybe metal. 28 00:01:06,660 --> 00:01:07,560 Cool. 29 00:01:07,620 --> 00:01:15,420 Got a bar of gold and now we've got to make Sly Dog or any NPC, any name of choice. 30 00:01:15,420 --> 00:01:22,170 I'm going to go to my toolbox home toolbox, and I'm just going to get Ralph and I'm going to rename 31 00:01:22,170 --> 00:01:22,590 them. 32 00:01:22,740 --> 00:01:24,300 And there are some waypoints there. 33 00:01:24,300 --> 00:01:25,590 Oh yeah, we have two scripts. 34 00:01:25,590 --> 00:01:26,340 That's all right. 35 00:01:26,340 --> 00:01:27,030 We could change. 36 00:01:27,030 --> 00:01:30,420 Ralph I probably should just saved them off without the waypoints. 37 00:01:30,690 --> 00:01:33,600 Let's call Ralph something else. 38 00:01:33,600 --> 00:01:35,550 We can call them slaw dog. 39 00:01:36,270 --> 00:01:38,670 Let's eat hot dogs with co slaw. 40 00:01:38,850 --> 00:01:39,600 There we go. 41 00:01:39,600 --> 00:01:41,460 And I'm going to get rid of the thumbnail camera. 42 00:01:41,460 --> 00:01:42,630 Should I save that? 43 00:01:42,630 --> 00:01:44,280 I'm going to get rid of the waypoints. 44 00:01:44,310 --> 00:01:48,230 We're going to generate our own and move NPC. 45 00:01:48,240 --> 00:01:51,090 I'm going to get rid of everything but the char. 46 00:01:53,210 --> 00:01:56,870 Let's define the rest of our variables that we're going to need for pathfinding. 47 00:01:56,960 --> 00:01:59,060 I'm going to start out with the treasure. 48 00:01:59,570 --> 00:01:59,870 Right. 49 00:01:59,930 --> 00:02:02,450 And remember what we have in the workspace, right? 50 00:02:02,480 --> 00:02:07,810 Remember, treasure is just a part and humanoid root parts are just parts. 51 00:02:07,820 --> 00:02:14,960 So if you blend the last video of this video, you could use pathfinding to chase players around and 52 00:02:14,960 --> 00:02:20,840 other non playing characters by using that fine closest HR app or fine closes char that we did in the 53 00:02:20,840 --> 00:02:21,680 last video. 54 00:02:21,680 --> 00:02:26,630 So get a little bit creative and start blending this stuff, but I'm just using a part to keep it simple 55 00:02:26,630 --> 00:02:27,350 for now. 56 00:02:27,650 --> 00:02:27,950 All right. 57 00:02:27,950 --> 00:02:37,790 Now what I'm going to need is the path finding service right game, get service, pathfinding service, 58 00:02:38,780 --> 00:02:43,880 and that's going to allow me to compute my waypoints between me and where I want to go. 59 00:02:43,880 --> 00:02:45,230 In this case, the treasure. 60 00:02:45,830 --> 00:02:48,010 I'm going to need a path, right? 61 00:02:48,020 --> 00:02:55,250 I'm going to get the path from the path finding service path, finding service, colon and create path. 62 00:02:55,520 --> 00:02:56,140 There you go. 63 00:02:56,180 --> 00:02:57,620 It gives you a little explanation. 64 00:02:59,520 --> 00:03:00,500 What else are we going to need? 65 00:03:00,500 --> 00:03:04,190 Oh, we're going to need to keep track of the waypoints we're on when we're running around. 66 00:03:04,190 --> 00:03:06,080 So I'm going to do a waypoint index. 67 00:03:06,080 --> 00:03:08,060 I'm going to initialize it to one. 68 00:03:08,570 --> 00:03:08,980 Right. 69 00:03:09,110 --> 00:03:14,360 And we're also going to need a waypoints table. 70 00:03:14,420 --> 00:03:18,890 We'll just make it empty to store our waypoints when we compute them. 71 00:03:19,850 --> 00:03:21,320 What else am I going to need? 72 00:03:21,350 --> 00:03:22,490 I got my character. 73 00:03:22,850 --> 00:03:24,030 I need the humanoid. 74 00:03:24,050 --> 00:03:26,840 I probably should have deleted it from Ralph, but that's all right. 75 00:03:26,840 --> 00:03:27,700 We'll just redo it. 76 00:03:27,710 --> 00:03:30,440 Chas, wait for child. 77 00:03:30,620 --> 00:03:33,320 I'll do a wait for child humanoid. 78 00:03:33,320 --> 00:03:36,410 And we need the help. 79 00:03:37,040 --> 00:03:39,710 Wait for child humanoid root part. 80 00:03:40,130 --> 00:03:43,160 Let's go down to lines and then define the function. 81 00:03:43,160 --> 00:03:48,590 So local function follow path underpass in the goal. 82 00:03:48,830 --> 00:03:49,200 Right? 83 00:03:49,250 --> 00:03:51,920 Or it could be the HRP if you were if you're chasing somebody. 84 00:03:51,920 --> 00:03:52,180 Right. 85 00:03:52,190 --> 00:03:52,850 That's a part. 86 00:03:52,850 --> 00:03:54,080 The goal is the part. 87 00:03:54,260 --> 00:04:00,500 And I'm going to get a I'm going to use my path variable and then compute a sync, right? 88 00:04:00,500 --> 00:04:03,740 Compute a sync. 89 00:04:03,740 --> 00:04:08,450 And I have to have the start and the stop and this is going to compute my path. 90 00:04:08,450 --> 00:04:16,160 So the start is me, my HPE position, my humanoid route, part position, and then the goal is where 91 00:04:16,160 --> 00:04:23,750 I want to go, but I need that position to and I'm going to re initialize my waypoint, Oh my gosh, 92 00:04:23,750 --> 00:04:30,770 my waypoints table right with this the curly brackets there, because I might call this over and over 93 00:04:30,770 --> 00:04:32,480 again and this might have waypoints in it. 94 00:04:32,480 --> 00:04:36,140 So let's just clear it out before we go to put new ones in. 95 00:04:36,650 --> 00:04:43,010 I'm going to do it if statement and I check my path status. 96 00:04:43,010 --> 00:04:43,400 Right. 97 00:04:43,400 --> 00:04:48,170 So I did the compute and now I want to know the status of that computation. 98 00:04:48,170 --> 00:04:57,170 We're going to compare it against this e nym path status, success to see if we have a success. 99 00:04:57,170 --> 00:04:58,790 Do we have a success? 100 00:04:59,240 --> 00:05:05,810 We'll do loops path colon, get waypoints. 101 00:05:08,300 --> 00:05:08,840 There we go. 102 00:05:08,840 --> 00:05:10,490 And that's only going to happen if there's a success. 103 00:05:10,490 --> 00:05:14,510 If there's a failure, this is going to leave the function and nothing's going to happen. 104 00:05:14,510 --> 00:05:18,800 So you'll have to do some checking if you want to, if you want it to be robust. 105 00:05:18,800 --> 00:05:19,220 Right. 106 00:05:19,220 --> 00:05:24,050 So now I want to get my w p index, make that one. 107 00:05:24,050 --> 00:05:33,500 We're going to start fresh on our waypoint path and then I'll say humanoid move to and I get my PES. 108 00:05:33,620 --> 00:05:38,630 This is very similar to our patrol, but we did this dynamically. 109 00:05:38,630 --> 00:05:42,140 We're going to get our waypoint index dot possession. 110 00:05:43,780 --> 00:05:44,340 All right. 111 00:05:44,350 --> 00:05:49,930 Now, the thing is, if we get here, we need to get to the next position. 112 00:05:49,930 --> 00:05:58,590 If you'll remember, two videos back, we did that, so we had this huge move to finish, right? 113 00:05:58,750 --> 00:06:02,350 And then we connected it to a function, an anonymous function. 114 00:06:02,350 --> 00:06:10,540 I did not do this in the in the simple patrol, but we have available to us a reached flag. 115 00:06:10,540 --> 00:06:16,150 So if the humanoid did make it to this point, this will be true. 116 00:06:16,150 --> 00:06:21,400 If it fell, if it timed out, if 8 seconds went by and didn't get there, this will be false. 117 00:06:21,400 --> 00:06:23,500 So you could do stuff with that, right? 118 00:06:23,500 --> 00:06:24,940 Like if you get stuck. 119 00:06:25,180 --> 00:06:32,770 So I'll say if reached and WP index is greater than. 120 00:06:33,670 --> 00:06:34,280 No. 121 00:06:34,390 --> 00:06:36,760 Is the less that we want to make sure there's more left. 122 00:06:37,300 --> 00:06:41,890 The number of waypoints in our table, then. 123 00:06:42,700 --> 00:06:46,690 Let's go ahead and say WP index plus equals one. 124 00:06:46,690 --> 00:06:55,180 Let's add let's go to the next index for our waypoints and then I need to check between every waypoint 125 00:06:55,180 --> 00:06:56,890 if I need to jump. 126 00:06:56,890 --> 00:06:58,690 So I'm going to do this. 127 00:06:58,710 --> 00:06:59,950 Woops. 128 00:07:01,490 --> 00:07:07,700 WP index dot action equals. 129 00:07:07,700 --> 00:07:19,780 So if the waypoint action equals E waypoint path action dot jump, we need to jump. 130 00:07:19,790 --> 00:07:20,240 So I'll say. 131 00:07:20,240 --> 00:07:21,800 HUME Jump. 132 00:07:21,800 --> 00:07:23,600 So we're adding jumping to this. 133 00:07:24,050 --> 00:07:24,710 True. 134 00:07:25,760 --> 00:07:26,270 All right. 135 00:07:26,270 --> 00:07:27,590 And then. 136 00:07:29,780 --> 00:07:32,300 We need to do to move to the next waypoint. 137 00:07:32,330 --> 00:07:37,640 WP -- WP index dot possession. 138 00:07:38,120 --> 00:07:38,920 Cool. 139 00:07:38,930 --> 00:07:40,330 Now that should work. 140 00:07:40,340 --> 00:07:45,500 The only thing I'm not handling right now is if something dynamically blocks our path. 141 00:07:45,500 --> 00:07:46,820 But we're going to do that. 142 00:07:46,820 --> 00:07:48,530 We're going to do that in the next video. 143 00:07:48,530 --> 00:07:50,230 It's not going to be that hard. 144 00:07:50,240 --> 00:07:52,880 This is the meat of it and we're not going to start over. 145 00:07:52,880 --> 00:07:54,650 We're not going to have to delete anything. 146 00:07:55,100 --> 00:08:01,490 I do need to test this, though, and we don't have any update loops or anything, so we'll just call 147 00:08:01,490 --> 00:08:01,850 it once. 148 00:08:01,850 --> 00:08:10,220 We'll wait 5 seconds and then we'll do a follow path, pass in the treasure and that we should find 149 00:08:10,220 --> 00:08:10,310 it. 150 00:08:10,310 --> 00:08:11,870 So let's go here. 151 00:08:13,020 --> 00:08:14,480 There's our little guy. 152 00:08:14,490 --> 00:08:15,870 We're going to spawn here. 153 00:08:15,870 --> 00:08:19,740 I move this up so we could see it, so we could see the action. 154 00:08:23,360 --> 00:08:24,890 And let's see. 155 00:08:26,400 --> 00:08:26,800 Ah! 156 00:08:27,180 --> 00:08:27,860 Hey, where are you going? 157 00:08:27,870 --> 00:08:28,330 Oh, man. 158 00:08:28,350 --> 00:08:29,460 Oh, you ran around it. 159 00:08:30,900 --> 00:08:32,070 Oh, that's kind of funny. 160 00:08:33,600 --> 00:08:38,220 The AI outsmarted me, but he made it to the treasure, so that's cool. 161 00:08:41,090 --> 00:08:44,030 Well, let's go ahead and move them and just check to see if he can go through the maze. 162 00:08:44,060 --> 00:08:44,980 I want to see him jump. 163 00:08:44,990 --> 00:08:50,750 So let's get him a little closer to entice him to go through the maze. 164 00:08:51,520 --> 00:08:52,630 Maybe like here. 165 00:08:52,630 --> 00:08:54,080 I don't want to run around the other way. 166 00:08:54,100 --> 00:08:55,570 That would be frustrating. 167 00:08:58,650 --> 00:08:59,490 Here we go. 168 00:09:01,460 --> 00:09:02,350 There you go. 169 00:09:02,360 --> 00:09:03,650 Run through the maze. 170 00:09:05,080 --> 00:09:05,980 Andy jumped. 171 00:09:06,010 --> 00:09:06,940 That's cool. 172 00:09:07,850 --> 00:09:08,120 All right. 173 00:09:08,120 --> 00:09:14,300 So the next video will add, We'll have to do a little trigger and then make a door pop up, maybe make 174 00:09:14,300 --> 00:09:16,100 this get bigger or something like that. 175 00:09:16,100 --> 00:09:20,090 And then he has to re compute his waypoint pass to get to his treasure. 176 00:09:20,090 --> 00:09:22,220 So you'll have to run around it then. 177 00:09:22,220 --> 00:09:23,360 That'll be cool.